package com.controller;

import java.io.IOException;
import java.io.PrintWriter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.bean.Client;
import com.bean.Complex;
import com.bean.Marketing;
import com.bean.Openingreport;
import com.bean.Project;
import com.bean.Receive;
import com.bean.Room;
import com.bean.Unit;
import com.bean.User;
import com.service.ClientService;
import com.service.ComplexService;
import com.service.MarketingService;
import com.service.OpeningreportService;
import com.service.ProjectService;
import com.service.ReceiveService;
import com.service.RoomService;
import com.service.UnitService;
import com.service.UserService;




@Controller
@RequestMapping("/marketing")
public class MarketingController {
	@Autowired
	private MarketingService service;
	
	@Autowired
	private ReceiveService rs;
	
	
	@Autowired
	private UserService us;
	
	
	@Autowired
	private ClientService cs;
	
	
	@Autowired
	private OpeningreportService os;
	
	@Autowired
	private ProjectService ps;
	
	
	@Autowired
	private ComplexService cos;
	
	
	@Autowired
	private UnitService uns;
	
	@Autowired
	private RoomService ros;
	

	
   //页面展示列表
	@RequestMapping("/marketing.do")
    public String marketing(HttpServletRequest re,Model model){
		Integer uid = (Integer) re.getSession().getAttribute("uid");
		Integer rid = (Integer) re.getSession().getAttribute("rid");
		
		System.out.println("rid+"+rid);
		List<Marketing> list=new ArrayList<Marketing>();
		List<String> list1=new ArrayList<String>();
		if(rid==1)
		{
			 list = service.selectAllByUserid(uid);	
		
		}
		if(rid==2)
		{
		
		 list = service.selectAll();
		
		
		}
		System.out.println(list.size());
		
		model.addAttribute("marketinglist",list);
	
    	return "marketing/list";
    }

	  
	@RequestMapping("/add.do")
	 public String add(HttpServletRequest re,Model model){
		List<User> list =new ArrayList<User>();
		

		List<Receive> list2=new ArrayList<Receive>();
		List<Client> list3=new ArrayList<Client>();
		
		Integer uid = (Integer) re.getSession().getAttribute("uid");
		Integer rid = (Integer) re.getSession().getAttribute("rid");
		
		if(rid==1)
		{
			System.out.println("userid=1111");
			
		list2 = rs.selectByuid(uid);
		for(Receive l:list2){
			Integer integer = l.getClientid();
			
			
			Client client = cs.selectOne(integer);
			
			list3.add(client);
		}
		  User user=new User();
		  user.setId(uid);
		  list.add(user);
		}
		if(rid==2)
		{
			System.out.println("userid=2222");
		  list2 = rs.selectALL();
		  for(Receive l:list2){
				Integer integer = l.getClientid();
				Client client = cs.selectOne(integer);
				
				list3.add(client);
			}
			list = us.selectAllByid();
		}
		
		
		
		List<Openingreport> list4 = os.selectByState("已审核");
		List<Project> list1=new ArrayList<Project>();
		for(Openingreport l:list4){
			Integer pid = l.getPid();
			Project project = ps.selectByPrimaryKey(pid);
			list1.add(project);
		}
		
		model.addAttribute("list1",list1);
		System.out.println("list1size="+list1.size());
		model.addAttribute("list",list);
		
		System.out.println("listsize="+list.size());
		
		model.addAttribute("list3",list3);
		System.out.println("list3size="+list3.size());
  	return "marketing/zadd";
  	}
	
	
	
	
	@RequestMapping("/addxin.do")
	 public String addxin(Integer id,Model model,HttpServletRequest re){
		Receive receive = rs.selectOne(id);	
		Integer userid = receive.getUserid();
		Integer clientid = receive.getClientid();
		
		User user = us.selectByPrimaryKey(userid);
		Client client = cs.selectOne(clientid);
		
		List<Openingreport> list = os.selectByState("已审核");
		List<Project> list1=new ArrayList<Project>();
		for(Openingreport l:list){
			Integer pid = l.getPid();
			Project project = ps.selectByPrimaryKey(pid);
			list1.add(project);
		}
		
		model.addAttribute("receive",receive);
		model.addAttribute("username",user.getUsername());
		model.addAttribute("client",client);
		model.addAttribute("list1",list1);
 	return "marketing/add";
 	}
	
	
	
	@RequestMapping("/toajax11.do")
	public void toajax(Integer cid,PrintWriter out,HttpServletRequest re) throws IOException{
	
		
		Client selectOne = cs.selectOne(cid);
		 
		 
				out.print("<option value="+selectOne.getPhone()+">---"+selectOne.getPhone()+"----</option>");
				
		
	}
	
	
	
	@RequestMapping("/toajax2.do")
	public void toajax2(Integer project,PrintWriter out,HttpServletRequest re) throws IOException{
		
		List<Complex> list = cos.selectByProjectId(project);
		
		for(Complex l:list){
			out.print("<option value="+l.getId()+">------"+l.getComplexnumber()+"-----</option>");
			
		}
		
		
	}
	
	
	@RequestMapping("/toajax3.do")
	public void toajax3(Integer complex,PrintWriter out,HttpServletRequest re) throws IOException{
		
	
		List<Unit> list = uns.selectByComplexId(complex);
		
		for(Unit l:list){
			out.print("<option value="+l.getId()+">--------"+l.getUnitenumber()+"--------</option>");
			
		}
		
		
	}
	
	@RequestMapping("/toajax4.do")
	public void toajax4(Integer unit,PrintWriter out,HttpServletRequest re) throws IOException{
		
		System.out.println("unit+"+unit);
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("unit",unit);
		map.put("state","已开盘");	
		List<Room> list = ros.selectByuidState(map);
		System.out.println("size()+"+list.size());
		for(Room l:list){
			out.print("<option value="+l.getId()+">--------"+l.getRoomnumber()+"--------</option>");
			
		}
		
		
	}
	
	
	
	@RequestMapping("/toajax1.do")
	public void toajax1(Integer userid,PrintWriter out,HttpServletRequest re) throws IOException{
		System.out.println("userid="+userid);
		
		   User user = us.selectByPrimaryKey(userid);
		   String name = user.getUsername();
		out.print(name);
	}
	
	
	
	@RequestMapping("/toajax5.do")
	public void toajax5(Integer mid,PrintWriter out,HttpServletRequest re) throws IOException{
		Marketing marketing = service.selectByPrimaryKey(mid); 
		System.out.println(marketing.getRoomid());
		Integer roomid = marketing.getRoomid();	
		Room room = ros.selectByPrimaryKey(roomid);		
		out.print(room.getState());
	}
	
	
	
	
	
	
	
	
	@RequestMapping("/addMarketing.do")
	 public String addMaketing(Marketing record,Model model){
		Integer roomid = record.getRoomid();
		record.setState("草稿");
		service.insert(record);
 	return "redirect:/marketing/marketing.do";
 	}
	
	
	@RequestMapping("/zaddMarketing.do")
	 public String zaddMarketing(Integer clientname,Marketing record,Model model){
	
	  Integer id = clientname;
	  Integer roomid = record.getRoomid();
	  System.out.println("roomid+"+roomid);
	   record.setClientid(id);
		service.insert(record);
	return "redirect:/marketing/marketing.do";
	}
	
	
	
	@RequestMapping("/update.do")
	 public String update(Integer ace,Model model){
		List<Openingreport> list4 = os.selectByState("已审核");
		List<Project> list1=new ArrayList<Project>();
		for(Openingreport l:list4){
			Integer pid = l.getPid();
			Project project = ps.selectByPrimaryKey(pid);
			list1.add(project);
		}
		Marketing marketing = service.selectByPrimaryKey(ace);
		
		Integer p = marketing.getProject();
		Project project2 = ps.selectByPrimaryKey(p);
		Integer complex = marketing.getComplex();
		Complex complex2 = cos.selectByPrimaryKey(complex);
		Integer unit = marketing.getUnit();
		Unit unit2 = uns.selectByPrimaryKey(unit);
		 Integer roomid = marketing.getRoomid();
		Room room = ros.selectByPrimaryKey(roomid);
		
		Integer userid = marketing.getUserid();
		User user = us.selectByPrimaryKey(userid);
		
		Integer cid = marketing.getClientid();
	     Client client = cs.selectOne(cid);
		
		
	     model.addAttribute("client",client);
		model.addAttribute("user",user);
		model.addAttribute("room",room);
		model.addAttribute("unit",unit2);
		model.addAttribute("project",project2);
		model.addAttribute("complex",complex2);
		model.addAttribute("marketing",marketing);
		model.addAttribute("list1",list1);
	return "marketing/update";
	}
	
	
	@RequestMapping("/updateMarketing.do")
	 public String updateMarketing(Integer id,Integer cid,Marketing marketing,Model model){
		
		
		Marketing marketing2 = service.selectByPrimaryKey(id);
		marketing.setId(id);
		marketing.setClientid(cid);
      int i = service.updateByPrimaryKey(marketing);
    	
	  return "redirect:/marketing/marketing.do";
	}
	
	
	
	
	@RequestMapping("/delete.do")
	 public String delte(Integer ace,Model model){

		System.out.println("mid="+ace);
		Marketing marketing = service.selectByPrimaryKey(ace);
		Integer roomid = marketing.getRoomid();
		System.out.println("roomid="+roomid);
		Map<String,Object> m = new HashMap<String,Object>();
		m.put("id",roomid);
		m.put("state","已开盘");
		ros.updateByidstate(m);
		
	    service.deleteByPrimaryKey(ace);
		
	return "redirect:/marketing/marketing.do";
	}
  

	
	
	
	@RequestMapping("/select.do")
	 public String select(HttpServletRequest re,String clientname,String name,Model model){
		Integer uid = (Integer) re.getSession().getAttribute("uid");
		Integer rid = (Integer) re.getSession().getAttribute("rid");
		
		
		List<Marketing> list=new ArrayList<Marketing>();
		
		if(uid==1)
		{
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("clientname",clientname);
			map.put("name", name);
			map.put("userid", uid);
		   list = service.selectBy(map);
		}
		if(uid==2){
		
			Map<String,Object> map = new HashMap<String,Object>();
			
			map.put("clientname", clientname);
			map.put("name", name);
		   list = service.selectBycnameuname(map);
			
		}
		
		
		
		model.addAttribute("marketinglist",list);	
 	return "marketing/list";
 	}
	
	
}
